package com.suisung.shopsuite.pay;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class NihaopaySecurePay {

    public static void main(String[] args) throws Exception {
        String token = "816151c8f7520ccff745c169e2c95fd8dce8c29dbe338d079d9090174641e1f0";
        String url = "https://api.nihaopay.com/v1.2/transactions/securepay";
        String urltest = "https://apitest.nihaopay.com/v1.2/transactions/securepay";
        String callback_url = "https://www.haioushop.com/member/order/detail?on=HO-202400715-6";
        String data = "rmb_amount=1000&currency=HKD&vendor=wechatpay&in_wechat=true&reference=HO-202400715-6&terminal=WAP&ipn_url=https://www.haioushop.com/front/pay/consumeDeposit/niHaoPayCallback?on=HO-202400715-6&callback_url="+callback_url;

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");

        con.setRequestProperty("Authorization", "Bearer " + token);
        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(data);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        System.out.println(response.toString());
    }
}



